package com.dqhl.community.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by guan on 2016/9/11.
 * 更换性别 activity
 */
public class UpdataSexActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_top_back;
    private TextView tv_top_title;
    private TextView tv_confirm;
    private RadioGroup radioGroup;
    private RadioButton rb_man, rb_women;
    private String Tag = "1";//（1为男，2为女)
    private String strSex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updata_sex);
        initView();
    }

    private void initView() {
        strSex = getIntent().getStringExtra("sex");

        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        iv_top_back.setVisibility(View.VISIBLE);
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("修改性别");
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        rb_man = (RadioButton) findViewById(R.id.rb_men);
        rb_women = (RadioButton) findViewById(R.id.rb_women);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);

        iv_top_back.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (rb_man.getId() == i){
                    Tag = "1";
                } else if (rb_women.getId() == i){
                    Tag = "2";
                }
            }
        });

        if ("男".equals(strSex)) {
            Tag = "1";
            rb_man.setChecked(true);
            rb_women.setChecked(false);
        } else {
            Tag = "2";
            rb_women.setChecked(true);
            rb_man.setChecked(false);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.tv_confirm:
                updateSex();
                break;
        }
    }

    //更换性别
    public void updateSex() {
        Dlog.e("GGgetString(Constant.SEX" + spUserInfo.getString(Constant.SEX, ""));
        if (Tag.equals(spUserInfo.getString(Constant.SEX, ""))) {
            toast("已经是这个性别了哦！");
        } else {
            Dlog.e("GG选择男女" + Tag);
            showCustomLoadBar(R.string.loading);
            RequestParams params = new RequestParams(Config.Api.modifySex);
            params.addBodyParameter("userid", app.getUserId());
            params.addBodyParameter("sex_new", Tag + "");
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e("GG修改性别返回" + result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getInfo(result, "errMsg");
                    if (errCode == 0) {
                        if (Tag == "1") {
                            strSex = "男";
                        } else {
                            strSex = "女";
                        }
                        SharedPreferences.Editor editor = spUserInfo.edit();
                        editor.putString(Constant.SEX, strSex);
                        editor.apply();
                        sendUpdateSexBroadCast();
                        toast("修改成功");
                        finish();
                    } else {
                        toast("修改失败");
                        finish();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    hideCustomLoadBar();
                }
            });

        }
    }

    /**
     * 发送一个广播
     */
    private void sendUpdateSexBroadCast() {
        Intent intent = new Intent(Constant.ACTION_UPDATE_SEX);
        intent.putExtra(Constant.SEX, strSex);
        sendBroadcast(intent);
    }
}
